import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import router from './router'
import { getStoredAuth, isTokenExpired, clearStoredAuth } from '@/utils/auth'
import './style.css'
import App from './App.vue'

// 应用启动时的初始化逻辑
const initializeApp = () => {
  console.log('应用启动，初始化中...')
  const { token, expireAt } = getStoredAuth()
  if (token && isTokenExpired(token, expireAt)) {
    console.log('检测到过期 token，清理本地认证信息')
    clearStoredAuth()
  }
  console.log('应用初始化完成')
}

// 在应用启动时执行初始化
initializeApp()

const app = createApp(App)

// 注册Element Plus图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

app.use(ElementPlus)
app.use(router)
app.mount('#app')
